package edu.ynu.se.xiecheng.achitectureclass.dao;

import edu.ynu.se.xiecheng.achitectureclass.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.entity.SensorData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Repository
public interface SensorDataDao extends JpaRepository<SensorData, Long> {
    // 根据设备查找最新的一条数据
    Optional<SensorData> findFirstByDeviceOrderByCreateTimeDesc(Device device);
    
    // 根据设备查找所有数据
    List<SensorData> findByDevice(Device device);
    
    // 根据设备和时间范围查询历史数据
    @Query("SELECT s FROM SensorData s WHERE s.device.deviceSerial = :deviceId AND s.createTime >= :startTime ORDER BY s.createTime DESC")
    List<SensorData> findHistoryData(@Param("deviceId") String deviceId, @Param("startTime") Date startTime);
} 